package com.example.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;

import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import com.example.hybrid.JsBridge;

/**
 *
 */
public class X5WebView extends WebView {
    private Context mContext;
    public X5WebView(Context context) {
        super(context);
        this.mContext = context;
        setBackgroundColor(85621);
    }

    private WebViewClient client = new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            webView.loadUrl(url);
            return true;
        }
    };


    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.setWebViewClient(client);
        this.mContext = context;
        initWebViewSettings();
        this.getView().setClickable(true);
    }


    private void initWebViewSettings() {
        WebSettings mWebviewSettings = this.getSettings();
        mWebviewSettings.setJavaScriptEnabled(true);
        mWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebviewSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        mWebviewSettings.setSupportZoom(true);
        mWebviewSettings.setBuiltInZoomControls(false);
        mWebviewSettings.setUseWideViewPort(true);
        mWebviewSettings.setSupportMultipleWindows(true);
        mWebviewSettings.setAppCacheEnabled(true);
        mWebviewSettings.setDomStorageEnabled(true);
        mWebviewSettings.setGeolocationEnabled(true);
        mWebviewSettings.setSaveFormData(true);
        mWebviewSettings.setAllowFileAccess(true);
        mWebviewSettings.setAppCacheMaxSize(Long.MAX_VALUE);
        mWebviewSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        mWebviewSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        addJavascriptInterface(new JsBridge(mContext),"JS_TROOPS");
    }

}
